<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户ID" prop="user_id">
            <el-input
              v-model="queryParams.id"
              placeholder="请输入用户ID"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item label="审核状态" prop="user_type">
            <el-select
              v-model="queryParams.type"
              placeholder=""
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.fire_user_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="用户性别" prop="gender">
            <el-select
              v-model="queryParams.gender"
              placeholder=""
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.fire_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5" v-show="ids.length > 0">
            <el-button
              type="primary"
              plain
              size="mini"
              @click="batchUpdateUser"
            >批量上架</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" :selectable="isTableSelect"/>
          <el-table-column label="用户状态" align="center" key="status" prop="status" v-if="columns[0].visible" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <span>{{ dict.type.fire_user_real_status[scope.row.status].label }}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户编号" align="center" key="id" prop="id" v-if="columns[1].visible" />
          <el-table-column label="姓名" align="center" key="name" prop="name" v-if="columns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="用户昵称" align="center" key="nickname" prop="nickname" v-if="columns[3].visible" :show-overflow-tooltip="true" />
          <el-table-column label="年龄" align="center" key="age" prop="age" v-if="columns[4].visible" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[5].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.crtTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="微信号" align="center" key="wechat" prop="wechat" v-if="columns[6].visible" :show-overflow-tooltip="true" />
          <el-table-column label="是否推荐" align="center" key="isSuggest" prop="isSuggest" v-if="columns[7].visible" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <span>{{ dict.type.common_whether[scope.row.isSuggest].label }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="二维码"
            width="90px"
            key="headimgurl"
            prop="headimgurl"
            v-if="columns[8].visible"
          >
            <template slot-scope="scope">
              <ImagePreview :src="scope.row.qrcode" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="个性图片"
            width="90px"
            key="images"
            prop="images"
            v-if="columns[9].visible"
          >
            <template slot-scope="scope">
              <ImagePreview  :src="scope.row.images[0]" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="detailShow(scope.row)"
              >详细信息</el-button>
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.isSuggest === 0"
                @click="changeRecomend(scope.row, 'ok')"
              >增加推荐</el-button>
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.isSuggest === 1"
                @click="changeRecomend(scope.row, 'cancel')"
              >取消推荐</el-button>
<!--              <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">-->
<!--                <span class="el-dropdown-link">-->
<!--                  <i class="el-icon-d-arrow-right el-icon&#45;&#45;right"></i>更多-->
<!--                </span>-->
<!--                <el-dropdown-menu slot="dropdown" v-if="scope.row.isSuggest === 0">-->
<!--                  <el-dropdown-item command="handleRecomendOK">增加推荐</el-dropdown-item>-->
<!--                </el-dropdown-menu>-->
<!--                <el-dropdown-menu slot="dropdown" v-if="scope.row.isSuggest === 1">-->
<!--                  <el-dropdown-item command="handleRecomendCancel">取消推荐</el-dropdown-item>-->
<!--                </el-dropdown-menu>-->
<!--                <el-dropdown-menu slot="dropdown">-->
<!--                  <el-dropdown-item command="handleChangeSex">修改性别</el-dropdown-item>-->
<!--                </el-dropdown-menu>-->
<!--              </el-dropdown>-->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!--查看详情-->
    <el-dialog
      title="详细信息"
      :visible.sync="isDetail"
      width="60%">
      <el-descriptions class="margin-top" :column="3" size="medium" border>
        <el-descriptions-item>
          <template slot="label">
            用户ID
          </template>
          {{ detail.id }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            用户姓名
          </template>
          {{ detail.name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            昵称
          </template>
          {{ detail.nickname }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            出生年份
          </template>
          {{ detail.age }}年
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            电话
          </template>
          {{ detail.mobile }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            体重
          </template>
          {{ detail.weight }}kg
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            婚姻状况
          </template>
          <dict-tag :options="dict.type.fire_user_marry" :value="detail.ismarry"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            工作信息
          </template>
          <dict-tag :options="dict.type.fire_user_job" :value="detail.job"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            星座
          </template>
          <dict-tag :options="dict.type.fire_user_constellation" :value="detail.constellation"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            微信号
          </template>
          {{ detail.wechat }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            身高
          </template>
          {{ detail.high }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            房产
          </template>
          <dict-tag :options="dict.type.fire_user_house" :value="detail.house"/>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            车产
          </template>
          <dict-tag :options="dict.type.fire_user_car" :value="detail.car"/>
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            二维码
          </template>
          <ImagePreview :src="detail.qrcode" :width="50" :height="50"/>
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            个性照片
          </template>
          <div v-for="detail in detail.images" :key="detail" style="display: inline">
            <ImagePreview :src="detail" :width="50" :height="50"/>
          </div>
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            个人介绍
          </template>
          {{ detail.selfIntroduce }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            择偶标准
          </template>
          {{ detail.selectCriteria }}
        </el-descriptions-item>
      </el-descriptions>
      <span slot="footer" class="dialog-footer">
        <block v-if="detail.status === 0">
          <el-button type="primary" size="mini" @click="updateUser(detail.openid, 0)" v-if="detail.status === 0">上架</el-button>
          <el-popover
            placement="top"
            width="160"
            :value="visi"
          >
            <p><el-input v-model="reasonMsg" placeholder="请输入内容" size="mini"></el-input></p>
            <div style="text-align: right; margin: 0">
              <el-button type="primary" size="mini" @click="updateUser(detail.openid, 3)">确定</el-button>
            </div>
            <el-button slot="reference" size="mini">不通过审核</el-button>
          </el-popover>
        </block>
        <el-button type="primary" size="mini" @click="updateUser(detail.openid, 1)" v-else-if="detail.status === 1">下架</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, updateUserInfo, changeSex, pullUserIdToSuggest, delUserIdFromSuggest } from './service'
export default {
  name: "usr-man",
  dicts: [
    'fire_user_status',
    'fire_user_sex',
    'fire_user_marry',
    'fire_user_job',
    'fire_user_constellation',
    'fire_user_house',
    'fire_user_car',
    'common_whether',
    'fire_user_real_status'
  ],
  data() {
    return {
      // 选中数组
      ids: [],
      // 不通过审核的理由弹窗
      visi: false,
      // 不通过审核的理由
      reasonMsg: '',
      // 详情
      detail: {},
      // 查看详情弹窗的显隐
      isDetail: false,
      // 日期范围
      dateRange: [],
      // 用户表格数据
      userList: null,
      // 总条数
      total: 0,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        type: '',
        gender: '',
        id: ''
      },
      // 列信息
      columns: [
        { key: 0, label: `用户状态`, visible: true },
        { key: 1, label: `用户编号`, visible: true },
        { key: 2, label: `姓名`, visible: true },
        { key: 3, label: `用户昵称`, visible: true },
        { key: 4, label: `年龄`, visible: true },
        { key: 5, label: `创建时间`, visible: true },
        { key: 6, label: `微信号`, visible: true },
        { key: 7, label: `是否推荐`, visible: true },
        { key: 8, label: `二维码`, visible: true },
        { key: 9, label: `个性图片`, visible: true },
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.openid);
    },
    // 批量上架功能
    batchUpdateUser () {
      let _this = this;
      this.ids.forEach(item => {
        _this.updateUser(item, 0)
      })
    },
    // 判断表格是否可选
    isTableSelect (row) {
      return row.status !== 2;
    },
    /** 查询推荐用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
          const { data: { resultList, totalPages} } = response;
          this.userList = resultList || [];
          this.total = totalPages;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleChangeSex":
          this.changeSexFun(row)
          break;
        case "handleRecomendOK":
          this.changeRecomend(row, 'ok')
          break;
        case "handleRecomendCancel":
          this.changeRecomend(row, 'cancel')
          break;
        default:
          break;
      }
    },
    // 显示详情
    detailShow: function (obj) {
      this.detail = obj
      this.isDetail = true
    },
    // 上架下架的操作
    updateUser: function (openid, type) {
      let _this = this;
      let str = type === 3? `/${_this.reasonMsg}`: '/0';
      updateUserInfo({openid, type, str}).then(res => {
          if (res.data) {
            _this.reasonMsg = '';
            _this.visi = false;
            _this.isDetail = false;
            _this.getList();
            _this.$message({
              type: 'success',
              message: '成功了!'
            });
          }
        }
      )
    },
    changeSexFun (obj) {
      let _this = this
      changeSex({openid: obj.openid}).then(res => {
        if (res.data) {
          _this.$message({
            type: 'success',
            message: '修改成功了!'
          });
          _this.getInfo();
        }
        }
      )
    },
    // 增加推荐的接口
    changeRecomend: function (obj, type) {
      let _this = this;
      if (type === 'ok') {
        this.$modal.confirm('确定增加推荐吗？').then(function() {
          pullUserIdToSuggest({openid: obj.id}).then((res) => {
            if (res.data) {
              _this.getList();
              _this.$message({
                type: 'success',
                message: '增加推荐成功!'
              });
            } else {
              _this.$message({
                type: 'error',
                message: '增加失败！'
              });
            }
          })
        })
      } else if (type === 'cancel') {
        this.$modal.confirm('确定取消推荐吗？').then(function() {
          delUserIdFromSuggest({openid: obj.id}).then((res) => {
            if (res.data) {
              _this.getList();
              _this.$message({
                type: 'success',
                message: '取消推荐成功!'
              });
            } else {
              _this.$message({
                type: 'error',
                message: '取消失败！'
              });
            }
          })
        })
      }
    },
  }
}
</script>

<style scoped>

</style>
